package com.thinkwide.data.login;

import com.thinkwide.data.core.MFString;
import com.thinkwide.data.debug.MFDebug;
import com.thinkwide.data.filter.MFXFilter;

import javax.servlet.http.HttpServletRequest;

public class MFBaseLogin implements IMFLogin {

    public String className;
    public IMFLogin login;

    public MFBaseLogin() {
        HttpServletRequest request = MFXFilter.threadRequestLocal.get();
        className = request.getSession().getServletContext().getInitParameter("login-class");
        MFDebug.println(className);
        try {
            login = (IMFLogin) getObject(className);
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public Object getObject(String ClassName) throws Throwable {
        Object obj = null;
        if (MFString.notNullOrEmpty(ClassName)) {
            Class<?> c = Class.forName(ClassName);
            obj = c.newInstance();
        }
        return obj;
    }

    public boolean logData(String userId, String type) {
        try {
            //IMFLogin login=(IMFLogin)getObject(className);
            return login.logData(userId, type);
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    public boolean logout(String userId) {
        try {
            //IMFLogin login=(IMFLogin)getObject(className);
            return login.logout(userId);
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    public MFUser getLoginUser(String userId) {
        MFUser user = null;
        try {
            //IMFLogin login=(IMFLogin)getObject(className);
            user = login.getLoginUser(userId);
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return user;
    }

    public boolean login(String userId) {
        // TODO Auto-generated method stub
        try {
            //IMFLogin login=(IMFLogin)getObject(className);
            return login.login(userId);
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    public boolean logData(String userId, String type, boolean inout) {
        // TODO Auto-generated method stub
        return false;
    }


}
